<?php

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use think\Loader;
class HeadlineUsedOrder extends Controller{
    /****
     * Class register          	      订单评论
     * @$post['order_id']             订单id
     * @$post['courier_id']        	  快递员id
     * @$post['content']              类型：0=差评，1=一般，2=好评
     * @$post['serve_grade']          店铺评分
     * @$post['superior_id']          上级id
     * @$post['taste_grade']          味道评分
     * @$post['pack_grade']           包装评分
     * @$post['manner_grade']         服务评分
     * @$post['like']                 0=差，1=点赞
     * @$post['anonymity']            状态：0=匿名，1=不匿名
     * @$post['image']                评论图片

     * @date                	        2020/06/03 
     * @package application\index\controller\OrderComment
    */
    public function index(){  
      $post = input('post.');  
	  $order_number = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
      $data=[
        'headline_used_id'     =>$post['headline_used_id'],
        'type'    			   =>$post['type'],
        'status'           	   =>$post['status'],
        'shipping_fee'         =>$post['shipping_fee'],
		'orderid'              =>'ZTXY'.$order_number,
        'order_time'    	   =>time(),
        'courier_id'           =>$post['courier_id'],
        'user_id'              =>$post['user_id'],
        'content'              =>$post['content'],
		'total_price'          =>$post['total_price'],
		'site_id'			   =>$post['site_id']

      ];    
      $order= Db::name('headline_used_order')->insert($data);
      if($order){
      	$order_find = Db::name('headline_used_order')->where(['user_id'=>$post['user_id']])->order('id desc')->find(); 
         $arr = [
           'state'      => 1,
           'order_find'	=>$order_find,
           'message'    => "请求成功"
        ];
      }else{
        $arr = [
           'state'      => 0,
           'message'    => "请求成功"
        ];
      }
        return json($arr);
    }
	 
	public function coupon(){
		$user_id = input('user_id');
		$id = input('id');
		$coupon = Db::name('coupon')->where(['user_id'=>$user_id,'id'=>id])->find();
		$arr = [
           'state'      => 1,
           'message'    => "请求成功"
        ];
		return json($arr);
	}
	
	// public function check(){
      // $user_id = input('user_id');
	  // $order = Db::name('headline_used_order a')->where(['user_id'=>$user_id])
	  // ->join('headline_used b','a.headline_used_id = b.id','LEFT')
	  
	  // ->find();
		
		
	  // return json($order);	  
    // }
    /***余额支付***/       
    public function pay(){    
     $user_id = input('user_id');
     $total_price = input('total_price');
     $id = input('id');
	 $coupon_id = input('coupon_id');
	 $integral = input('integral');
	 $type = input('type');
     $user = Db::name('user')->where(['id'=>$user_id])->find();
     if($user['money'] >= $total_price){  
		  if($type == 10){  
			  if($user['score'] >= $integral){
				  $integral_order = $user['score'] - $integral;
				  $data = [
						'score'=>$integral_order,
				  ];
			      Db::name('user')->where(['id'=>$user_id])->update($data);
				  Db::name('integral_order')->where(['id'=>$id])->update(['status'=>1]);
				  $data_integral = [
					'credits'	  =>$user['score'],
					'the_integral'=>$integral,
					'addtime'	  =>time(),
					'pid'		  =>$id,
					'user_id'	  =>$user_id,
					'type'		  =>0,
				  ];
				  Db::name('integral_log')->insert($data_integral);
				  $arr = [
					'state'   =>1,
					'message' =>'支付成功！'
				  ];
			  }else{
				$arr = [
					'state'   =>0,
					'message' =>'积分不足！'
				];  
			  }
			  return json($arr);
		  }else{
			$money = $user['money'] - $total_price;
			$score = $user['score'] + $total_price;		  
			$data = [
				'money'=>$money,
				'score'=>$score
			];
			Db::name('user')->where(['id'=>$user_id])->update($data);
			Db::name('order')->where(['id'=>$id])->update(['status'=>1]);
			Db::name('coupon')->where(['id'=>$coupon_id])->delete();
			$coupon_data=[
				'coupon_id'=>$coupon_id,
				'addtime'=>time(),
				'user_id'=>$user_id,
			];
			$this->printer($id);
			Db::name('coupon_log')->insert($coupon_data);
		  }
          $arr = [
            'state'   =>1,
            'message' =>'支付成功！'
          ];
     }else{
          $arr = [
            'state'   =>0,
            'message' =>'余额不足！'
          ];
     }
        return json($arr);
    }    
	


}
